<template>
  <div>
    <form-marker-biserial
      :isEdit="isEditPage"
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData"
      v-on:on-validate="onValidate"
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker-biserial>
  </div>
</template>
<script>
import FormMarkerBiserial from '@/components/createPage/createBiserial'

export default {
  components: {
    FormMarkerBiserial
  },
  data () {
    // 自定义验证规则
    let checkForepartType = (rule, value, callback) => {
      if (this.meanwhileNull) {
        callback(new Error('前段、中段、后端类型不能同时为空'))
      } else {
        callback()
      }
    }
    // 自定义验证规则
    let checkInteger = (rule, value, callback) => {
      let reg = /^(0|\+?[1-9][0-9]*)$/g
      if (value && !reg.test(value)) {
        callback(new Error('请输入数字'))
      } else if (value && value.length > 11) {
        callback(new Error('长度不可超过11位'))
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          leftChild: [
            {
              type: 'select',
              label: '对应公司',
              prop: 'companyCode',
              propName: 'companyName',
              optionLabel: 'name',
              optionValue: 'code'
            },
            {
              type: 'select',
              label: '前段类型',
              prop: 'forepartType',
              propName: 'forepartTypeName',
              optionLabel: 'dicValue',
              optionValue: 'dicCode'
            },
            {
              type: 'select',
              label: '中段类型',
              prop: 'middleType',
              propName: 'middleTypeName',
              optionLabel: 'dicValue',
              optionValue: 'dicCode'
            },
            {
              type: 'select',
              label: '后段类型',
              prop: 'posteriorType',
              propName: 'posteriorTypeName',
              optionLabel: 'dicValue',
              optionValue: 'dicCode'
            },
            {
              type: 'switch',
              label: '是否增加库存',
              prop: 'isAddAmount'
            },
            {
              type: 'switch',
              label: '是否消减库存',
              prop: 'isMinusAmount'
            },
            {
              type: 'textarea',
              label: '备注',
              name: '备注',
              prop: 'remark',
              inputType: 'textarea'
            }
          ],
          rightChild: [
            {
              type: 'select',
              label: '单据类型',
              prop: 'docType',
              propName: 'name',
              optionLabel: 'dicValue',
              optionValue: 'dicCode'
            },
            {
              type: 'input',
              label: '前段编号',
              prop: 'forepartNum'
            },
            {
              type: 'input',
              label: '中段编号',
              prop: 'middleNum'
            },
            {
              type: 'input',
              label: '后段编号',
              prop: 'posteriorNum'
            },
            {
              type: 'switch',
              label: '是否锁定库存',
              prop: 'isLockAmount'
            },
            {
              type: 'switch',
              label: '是否计算成本',
              prop: 'isCalCost'
            },
            {
              type: 'input',
              label: '自增长度',
              prop: 'selfIncrementNumLength'
            }
          ]
        }
      ],
      rules: {
        forepartType: [{ validator: checkForepartType, trigger: 'change' }],
        middleType: [{ validator: checkForepartType, trigger: 'change' }],
        posteriorType: [{ validator: checkForepartType, trigger: 'change' }],
        docType: [{ required: true, message: '单据类型不允许为空', trigger: 'change' }],
        selfIncrementNumLength: [
          { validator: checkInteger, trigger: 'blur' }
        ],
        posteriorNum: [{ max: 64, message: '长度不可超过64个字符', trigger: 'blur' }],
        middleNum: [{ max: 64, message: '长度不可超过64个字符', trigger: 'blur' }],
        forepartNum: [{ max: 64, message: '长度不可超过64个字符', trigger: 'blur' }],
        remark: [{ max: 255, message: '长度不可超过255个字符', trigger: 'blur' }]
      },
      selectData: {},
      detailData: {},
      meanwhileNull: false, // 前段、中段、后端类型不能同时为空
      noRepeat: false, // 编码唯一性
      isEditPage: false // 编辑or新增
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    this.getCompanySelect()
    this.getTypeSelect('bill_type', 'docType')
    this.getTypeSelect('generation_type', 'forepartType')
    this.getTypeSelect('generation_type', 'middleType')
    this.getTypeSelect('generation_type', 'posteriorType')
    this.getSeriaDetail()
  },
  methods: {
    // 获取company下拉列表数据
    async getCompanySelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findCompanyList`, {})
      this.selectData.companyCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取单据类型下拉列表数据
    async getTypeSelect (typeValue, keyValue) {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/findDictionaryByType`, {type: typeValue})
      this.selectData[keyValue] = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取编辑详情数据
    async getSeriaDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/serialNumber/findSNIDetail`, {sniCode: this.pageType})
        this.detailData = data
        this.isEditPage = true
      }
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = `/${this.$const.PREFIX.AUTHORITY}/serialNumber/operationData`
      let params = val
      let operateType = this.pageType === 'new' ? 'save' : 'edit'
      if (this.pageType === 'new') {
        params.isAddAmount = !params.isAddAmount ? false : params.isAddAmount
        params.isMinusAmount = !params.isMinusAmount ? false : params.isMinusAmount
        params.isCalCost = !params.isCalCost ? false : params.isCalCost
        params.isLockAmount = !params.isLockAmount ? false : params.isLockAmount
      }
      this.$http.post(url, {operateType: operateType, serialNumberInfo: params}).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'seriaNumber'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'seriaNumber'})
    },
    onValidate (val) {
      this.meanwhileNull = val
    }
  }
}
</script>
<style lang="scss" scoped>

</style>